package com.deke.home.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deke.entity.TbLink;
import com.deke.entity.TbZone;
import com.deke.home.dao.TbLinkDao;
import com.deke.home.service.TbLinkService;
import com.deke.home.vo.TbLinkVo;
import com.deke.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author ：ludi（206913808@qq.com）
 * @date ：Created in 2022/12/10 23:42
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class TbLinkServiceImpl implements TbLinkService {

    @Autowired
    private TbLinkDao tbLinkDao;


    /**
     * @param current
     * @param pageSize
     * @param tbLinkVo
     * @return com.deke.vo.Result<com.baomidou.mybatisplus.core.metadata.IPage < com.deke.entity.TbLink>>
     * @create by: ludi（206913808@qq.com）
     * @description: 分页查询、条件查询
     * @create time: 2022/12/10 23:46
     */
    @Override
    public Result<IPage<TbLink>> queryByConditionPage(Integer current, Integer pageSize, TbLinkVo tbLinkVo) {
        IPage<TbLink> page = new Page(current, pageSize);
        QueryWrapper<TbLink> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted", 0);
        if (StringUtils.hasText(tbLinkVo.getLinkId())) {
            wrapper.like("link_id", tbLinkVo.getLinkId());
        }
        if (StringUtils.hasText(tbLinkVo.getLinkName())) {
            wrapper.like("link_name", tbLinkVo.getLinkName());
        }
        if (ObjectUtil.isNotEmpty(tbLinkVo.getIsDisable())) {
            wrapper.like("is_disable", tbLinkVo.getIsDisable());
        }
        IPage<TbLink> iPage = tbLinkDao.selectPage(page, wrapper);
        return new Result<>(2000, "查询成功", iPage);
    }


    /**
     * @param linkId
     * @param isDisable
     * @return com.deke.vo.Result
     * @create by: ludi（206913808@qq.com）
     * @description: 改变状态
     * @create time: 2022/12/10 23:54
     */
    @Override
    public Result changeStatus(String linkId, Integer isDisable, TbLink tbLink) {
        UpdateWrapper<TbLink> wrapper = new UpdateWrapper<>();
        wrapper.eq("link_id", linkId);
        tbLink.setIsDisable(isDisable);
        int update = tbLinkDao.update(tbLink, wrapper);
        if (update == 1) {
            return new Result(2000, "状态改变成功");
        } else {
            return new Result(5000, "状态改变失败");
        }
    }


    /**
     * @param map
     * @return com.deke.vo.Result
     * @create by: ludi（206913808@qq.com）
     * @description: 删除操作
     * @create time: 2022/12/11 0:07
     */
    @Override
    public Result deleteLink(Map map, TbLink tbLink) {
        UpdateWrapper<TbLink> wrapper = new UpdateWrapper<>();
        wrapper.eq("link_id", map.get("linkId"));
        tbLink.setIsDeleted(1);
        int update = tbLinkDao.update(tbLink, wrapper);
        if (update == 1) {
            return new Result(2000, "删除成功");
        } else {
            return new Result(5000, "删除失败");
        }
    }


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 添加、编辑操作
     * @create time: 2022/12/11 0:17
     * @param tbLink
     * @return com.deke.vo.Result
     */
    @Override
    public Result addLink(TbLink tbLink) {
        if (ObjectUtil.isNotEmpty(tbLink.getLinkId())){
            UpdateWrapper<TbLink> wrapper = new UpdateWrapper<>();
            wrapper.eq("link_id", tbLink.getLinkId());
            int update = tbLinkDao.update(tbLink,wrapper);
            if (update==1){
                return new Result(2000, "修改成功");
            }else {
                return new Result(5000, "修改失败");
            }
        }else {
            Snowflake snowflake = IdUtil.createSnowflake(1, 1);
            String nextIdStr = snowflake.nextIdStr();
            tbLink.setLinkId(nextIdStr);
            tbLink.setIsDisable(0);
            tbLink.setIsDeleted(0);
            int insert = tbLinkDao.insert(tbLink);
            if (insert == 1) {
                return new Result(2000, "添加成功");
            } else {
                return new Result(5000, "添加失败");
            }
        }
    }
    
    
    /**
     * @create by: Username 刘亦辰 
     * @description: 前台查询link 
     * @create time: 2022/12/21 17:05
     * @param null 
     * @return :  
     **/
    @Override
    public Result getQianLink() {
        QueryWrapper<TbLink> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("is_disable",0);
        List<TbLink> linkList = tbLinkDao.selectList(wrapper);
        if (ObjectUtil.isNotEmpty(linkList) && linkList.size()>0){
            return new Result(2000,"成功",linkList);
        }
        return new Result(0000,"失败");
    }
}